﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace y12fjydbs_1
{

    public static class ExtensionMethod
    {
        public static TResult SafeInvoke<T, TResult>(this T isi, Func<T, TResult> call) where T : ISynchronizeInvoke
        {
            try
            {
                if (isi.InvokeRequired)
                {
                    IAsyncResult result = isi.BeginInvoke(call, new object[] { isi });
                    object endResult = isi.EndInvoke(result);
                    return (TResult)endResult;
                }
                else
                {
                    return call(isi);
                }
            }
            catch (Exception ee)
            {
                Msg.WriteLine(ee.Message, 50, '*');
                return  default(TResult);
            }
        }
        public static void SafeInvoke<T>(this T isi, Action<T> call) where T : ISynchronizeInvoke
        {
            if (isi.InvokeRequired)
            {
                isi.BeginInvoke(call, new object[] { isi });
            }
            else
            {
                call(isi);
            }
        }
    }
}
